package com.dacool.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.ImageView.ScaleType;
import android.widget.TextView;

import com.dacool.app.sale.R;

public class IconView extends ViewGroup {
	private RoundedImageView mImageView;
	private TextView mTextView;
	public static final String TAG = "IconView";

	public IconView(Context context) {
		this(context, null);
	}

	public IconView(Context context, AttributeSet attrs) {
		super(context, attrs);
		TypedArray a = context.obtainStyledAttributes(attrs,
				R.styleable.IconView);

		Drawable d = a.getDrawable(R.styleable.IconView_android_src);
		mImageView = new RoundedImageView(context);
		if (d != null)
			mImageView.setImageDrawable(d);
		int color = a.getInt(R.styleable.IconView_android_textColor,
				Color.BLACK);
		mTextView = new TextView(context);
		mTextView.setGravity(Gravity.CENTER);
		mTextView.setTextColor(color);
		mImageView.setBackgroundColor(Color.TRANSPARENT);
		// mImageView.setRoundBackground(true);
		mImageView.setScaleType(ScaleType.FIT_CENTER);
		mImageView.setCornerRadius(0);
		this.addView(mImageView);
		this.addView(mTextView);
		a.recycle();
	}
	
	public void setDrawableID(int id) {
		Drawable drawable = getContext().getResources().getDrawable(id);
		setDrawable(drawable);
	}

	public void setDrawable(Drawable drawable) {
		// Drawable rd = drawable;
		// if (drawable instanceof BitmapDrawable) {
		// Bitmap bm = ((BitmapDrawable) drawable).getBitmap();
		// bm = ViewUtils.circleBitmap(bm, 40, 10, 0xff85c400);
		// mImageView.setImageBitmap(bm);
		// } else
		mImageView.setImageDrawable(drawable);

	}

	public void setText(CharSequence text) {
		mTextView.setText(text);
	}


	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		measureChild(mImageView, widthMeasureSpec, heightMeasureSpec);
		measureChild(mTextView, widthMeasureSpec, heightMeasureSpec);
//		Log.d("test", "measure wdith " + widthMeasureSpec + ", height "
//				+ heightMeasureSpec);
//		Log.d("test",
//				String.format("w1 %d, w2 %d, h1 %d, h2 %d",
//						mImageView.getMeasuredWidth(),
//						mTextView.getMeasuredWidth(),
//						mImageView.getMeasuredHeight(),
//						mTextView.getMeasuredHeight()));
		setMeasuredDimension(
				(mTextView.getMeasuredWidth() > mImageView.getMeasuredWidth() ? mTextView.getMeasuredWidth()
						: mImageView.getMeasuredWidth()),
				mImageView.getMeasuredHeight() + mTextView.getMeasuredHeight());
	}

	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		// TODO Auto-generated method stub
		if (!changed)
			return;
		int size = r - l - 10;
		mImageView.layout(5, 3, size + 5, size + 3);
		mTextView.layout((r - l - mTextView.getMeasuredWidth()) / 2, size + 6,
				(r - l + mTextView.getMeasuredWidth()) / 2, b - t);
	}

}
